package com.jielihaofeng.atguigu.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * @author Johnnie Wind
 * @description 应用实例4 - 拷贝文件 transferFrom 方法
 * @date 2021/11/25 11:49
 */
public class NioFileChannel04 {
    public static void main(String[] args) throws Exception{
        // 创建文件输入输出流
        FileInputStream fileInputStream = new FileInputStream("d:\\a.jpg");
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\a2.jpg");

        // 获取各个流的channel
        FileChannel sourceCh = fileInputStream.getChannel();
        FileChannel destCh = fileOutputStream.getChannel();

        // 使用transferFrom完成拷贝
        destCh.transferFrom(sourceCh,0,sourceCh.size());

        // 关闭相关通道和流
        sourceCh.close();
        destCh.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}
